package com.blog.controller;

import com.blog.constant.MsgConstant;
import com.blog.pojo.dto.ReviewDTO;
import com.blog.pojo.entity.PageBean;
import com.blog.pojo.vo.ReviewVO;
import com.blog.result.Result;
import com.blog.service.ReviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/review")
public class ReviewController {
    @Autowired
    private ReviewService reviewService;

    /**
     * 分页查询评论
     * @param currentPage 当前页
     * @param pageSize 每页数据条数
     * @param articleTitle
     * @return
     */
    @GetMapping("/page")
    public Result<PageBean> getReviewPage(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "2") Integer pageSize,
            @RequestParam(required = false) String articleTitle,
            @RequestParam(required = false) String content,
            @RequestParam(required = false) Long articleId
            ){
        log.info("开始分页查询评论：{}, {}, {}, {}, {}", currentPage, pageSize, articleTitle, content, articleId);

        try {
            PageBean<ReviewVO> page = reviewService.getReviewPage(currentPage, pageSize,articleTitle, content, articleId);
            return Result.success(page);
        } catch (Exception e) {
            log.info("分页查询评论失败：{}", e);
            return Result.error(MsgConstant.REVIEW_SELECT_ERROR);
        }
    }

    /**
     * 添加评论
     * @param reviewDTO
     * @return
     */
    @PostMapping("/add")
    public Result addReview(@RequestBody ReviewDTO reviewDTO){
        log.info("开始添加评论：{}", reviewDTO);

        try {
            reviewService.addReview(reviewDTO);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.info("添加评论失败：{}", e);
            return Result.error(MsgConstant.REVIEW_ADD_ERROR);
        }
    }

    /**
     * 修改评论
     * @param reviewDTO
     * @return
     */
    @PutMapping("/update")
    public Result updateReview(@RequestBody ReviewDTO reviewDTO){
        log.info("开始修改评论：{}", reviewDTO);
        try {
            reviewService.updateReview(reviewDTO);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.info("修改评论失败：{}", e);
            return Result.error(MsgConstant.REVIEW_UPDATE_ERROR);
        }

    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteReview(@PathVariable Long id){
        log.info("开始删除评论：{}", id);

        try {
            reviewService.deleteReview(id);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.info("删除评论失败：{}", e);
            return Result.error(MsgConstant.REVIEW_DELETE_ERROR);
        }
    }

    /**
     * 批量删除评论
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteReviews(@RequestBody List<Long> ids){
        log.info("开始批量删除评论：{}", ids);

        try {
            reviewService.deleteReviews(ids);
            return Result.success(MsgConstant.SUCCESS_MSG);
        } catch (Exception e) {
            log.info("批量删除评论失败：{}", e);
            return Result.error(MsgConstant.REVIEW_DELETE_ERROR);
        }

    }

    /**
     * 获取评论数量
     * @return
     */
    @GetMapping("/count")
    public Result<Long> getReviewCount(){
        log.info("开始获取评论数量");

        try {
            Long count = reviewService.getReviewCount();
            return Result.success(count);
        } catch (Exception e) {
            log.error("获取评论数量失败：", e);
            return Result.error(MsgConstant.REVIEW_SELECT_ERROR);
        }
    }

}
